package com.secondhand.project.controller;

import com.secondhand.common.annotation.Log;
import com.secondhand.common.core.controller.BaseController;
import com.secondhand.common.core.domain.AjaxResult;
import com.secondhand.common.core.page.TableDataInfo;
import com.secondhand.common.enums.BusinessType;
import com.secondhand.common.utils.poi.ExcelUtil;
import com.secondhand.project.domain.Comment;
import com.secondhand.project.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/project/comment")
public class CommentController extends BaseController {
    @Autowired
    private ICommentService commentService;

    @PreAuthorize("@ss.hasPermi('project:comment:list')")
    @GetMapping("/tree")
    public AjaxResult selectTree(Comment comment) {
        List<Comment> commentList = commentService.selectTree(comment);
        return success(commentList);
    }

    @PreAuthorize("@ss.hasPermi('project:comment:list')")
    @GetMapping("/count")
    public AjaxResult selectCount(Comment comment) {
        Long count = commentService.selectCount(comment);
        return success(count);
    }

    @PreAuthorize("@ss.hasPermi('project:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Comment comment) {
        startPage();
        List<Comment> list = commentService.selectCommentList(comment);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('project:comment:export')")
    @Log(title = "评论信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Comment comment) {
        List<Comment> list = commentService.selectCommentList(comment);
        ExcelUtil<Comment> util = new ExcelUtil<Comment>(Comment.class);
        util.exportExcel(response, list, "评论信息数据");
    }

    @PreAuthorize("@ss.hasPermi('project:comment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(commentService.selectCommentById(id));
    }

    @PreAuthorize("@ss.hasPermi('project:comment:add')")
    @Log(title = "评论信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Comment comment) {
        return toAjax(commentService.insertComment(comment));
    }

    @PreAuthorize("@ss.hasPermi('project:comment:edit')")
    @Log(title = "评论信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Comment comment) {
        return toAjax(commentService.updateComment(comment));
    }

    @PreAuthorize("@ss.hasPermi('project:comment:remove')")
    @Log(title = "评论信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(commentService.deleteCommentByIds(ids));
    }
}
